package fenetre;

//////////////////////////////////////////IMPORTS
import javax.swing.*;

import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListModel;

import metier.CL_artiste;
import metier.CL_disque;
import metier.Catalogue;

import java.sql.ResultSet;
import java.util.*;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
//import java.awt.List;

public class CL_fenetre extends JFrame implements MouseListener , ActionListener{
	private static final long serialVersionUID = 1L;
	
	//ELEMENTS DE LA FENETRE
	private JTextField tfrecherche;
	private JButton btnNouvelAlbum;
	private JLabel lblHistoriqueDesActions;
	private JButton btnsupprimer;
	private JLabel lblRecherche;
	private JComboBox cbrecherche;
	private JButton btnRechercher;
	private JButton btnToutAfficher;
	private JButton btnModifier;
	private JList list_actions;
	//CLASSES
	private CL_ajt_cd ajoutcd;
	private CL_disque o_disque;
	private CL_artiste o_artiste;
	private Catalogue o_catalogue;
	//VARIABLES
	private ResultSet rs;
	private String[] cd;
	private Object[][] tab = {};
	private String[] title ={"ID","Album","Date","Artiste"};
	private JTable Table_Recherche;
	///CONSTRUCTEUR
	public CL_fenetre(){
		build();
	}
	/////////////////////////////
	private void build() {	
		o_artiste = new CL_artiste();
		o_catalogue = new Catalogue();
		//JFrame fenetre = new JFrame();
		setSize(634, 396);
		list_actions = new JList();
		btnModifier = new JButton("Modifier");
		
		btnNouvelAlbum = new JButton("Nouvel album");
		btnNouvelAlbum.addActionListener(this);
		btnNouvelAlbum.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) { }	
			});
		
		btnRechercher = new JButton("Rechercher");
		btnRechercher.addActionListener(this);
		btnRechercher.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) { }	
			});
		
		btnToutAfficher = new JButton("Tout afficher");
		btnToutAfficher.addActionListener(this);
		btnToutAfficher.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) { }	
			});
		
		btnsupprimer = new JButton("Supprimer");
		btnsupprimer.addActionListener(this);
		btnsupprimer.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) { }	
			});
		
		tfrecherche = new JTextField();
		tfrecherche.setColumns(10);
		lblRecherche = new JLabel("Recherche");
		cbrecherche = new JComboBox();
		cbrecherche.addItem("Disque");
		cbrecherche.addItem("Auteur");
		cbrecherche.addItem("Genre");

		
		lblHistoriqueDesActions = new JLabel("Historique des actions");
		
		Table_Recherche = new JTable(tab, title);
		
		////////////////////////////CONTRAINTES DE PLACEMENT DES ELEMENTS/////
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblRecherche)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(tfrecherche, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(cbrecherche, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(btnRechercher)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(btnToutAfficher, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(7)
									.addComponent(Table_Recherche, GroupLayout.PREFERRED_SIZE, 429, GroupLayout.PREFERRED_SIZE)))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(btnNouvelAlbum, GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
								.addComponent(list_actions, GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
								.addComponent(lblHistoriqueDesActions)
								.addComponent(btnsupprimer, GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
								.addComponent(btnModifier, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE))
							.addGap(7)))
					.addGap(16))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(16)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblRecherche)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(tfrecherche, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(btnRechercher)
								.addComponent(cbrecherche, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(btnToutAfficher)))
						.addComponent(lblHistoriqueDesActions))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(list_actions, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(btnsupprimer)
							.addGap(10)
							.addComponent(btnModifier)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnNouvelAlbum))
						.addComponent(Table_Recherche, GroupLayout.PREFERRED_SIZE, 279, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(14, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
	}

				


//////////////////////////////////////////////////////////////////////////METHODES///////////////////////////////////////
	public void m_method(){
		
	}
////////////////////////////////////////////////////////////////////////////////////ACTION_PERFORMED/////////////////////
	@SuppressWarnings("rawtypes")
	public void actionPerformed(ActionEvent e){
		String nomBouton = e.getActionCommand();

		if (nomBouton == "Nouvel album"){
			CL_ajt_cd ajoutcd = new CL_ajt_cd();
			ajoutcd.setSize(400, 350);
			ajoutcd.setTitle("Ajouter un cd");
			ajoutcd.setDefaultCloseOperation(ajoutcd.DISPOSE_ON_CLOSE);
			ajoutcd.setLocationRelativeTo(null);     
			ajoutcd.setVisible(true);
			ajoutcd.setResizable(false);
		}
		if (nomBouton == "Rechercher"){

			o_catalogue.m_recherche(tfrecherche.getText(), (String)cbrecherche.getSelectedItem());
		}		
		
		
		
		
		

		if (nomBouton == "Tout afficher")
		{
			
			System.out.println("afficher tout!");
			o_catalogue.m_afficher_catalogue(0);
			o_disque = new CL_disque();
			
			
			
			
			/*for(int i=0;i<o_catalogue.m_afficher_catalogue(0).length;i++)
			{
				for(int j=0;j<o_catalogue.m_afficher_catalogue(0)[i].length;j++)
				{
					System.out.println(o_catalogue.m_afficher_catalogue(0)[i][j]);
					tab[i][j] = o_catalogue.m_afficher_catalogue(0)[i][j];
				}
			}
			list_cd.setListData(o_catalogue.m_afficher_catalogue(0));
			tab = o_catalogue.m_afficher_catalogue(0);*/
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		if (nomBouton == "Supprimer"){
			CL_confirm confirm = new CL_confirm();
			confirm.setSize(300, 110);
			confirm.setTitle("Ajouter un cd");
			confirm.setLocationRelativeTo(null); 
			confirm.setDefaultCloseOperation(confirm.DISPOSE_ON_CLOSE);
			confirm.setVisible(true);
			confirm.setResizable(false);
		}
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
	}
	@Override
	public void mouseExited(MouseEvent arg0) {		
	}
	@Override
	public void mousePressed(MouseEvent arg0) {		
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {		
	}
	@Override
	public void mouseClicked(MouseEvent e) {	
		//int cc = e.getClickCount();
		//Object source = e.getSource();
	}
}
